#include "stdafx.h"
#include "GameplayState.h"
#include "../../../EdgeGameCore\World\ChunkManager.h"
#include "../Game.h"

#include "../../../EdgeGraphics/Camera.h"
#include "../../../EdgeMath/VectorHelper.h"
#include "../../../EdgeGameCore/SettingsManager.h"
#include "../Ui/InventoryMenu.h"
#include "../../../EdgeControls\Input.h"
#include "../.././../EdgeGraphics/RenderTargetManager.h"
#include "../../../EdgeControls\Controls.h"
#include <crtdbg.h>
#include "../../../EdgeGameCore/World/WorldEvents.h"
#include "../../../EdgeGraphics/BlendStateManager.h"
#include "../Ui/TempleMenu.h"
#include "../../../EdgeGameCore/Console.h"
/*static*/ GameplayState* GameplayState::instance = nullptr;

void show_inventory(void**);
void show_templeMenu(void**);

GameplayState::GameplayState(void)
{
	ChunkManager::Init();

	Camera::Inilitize(SettingsManager::GetInstance()->getSetting("ScreenWidth")._int, 
		SettingsManager::GetInstance()->getSetting("ScreenHeight")._int);

	menus = new MenuManager();
	menus->setActiveMenu(InventoryMenu::GetInstance());

	// subscribe for tab key pressed
	Input::GetInstance()->getKeyBindings(Controls::GetInstance().open_inventory)->Subscribe(Functor<key_event_handler>(&show_inventory, 0));

	WorldEvents::TempleClicked->Subscribe(Functor<world_event_handler>(&show_templeMenu, 0));
}

void show_templeMenu(void**)
{
	if (Input::GetInstance()->wasKeyDown(Controls::GetInstance().interact.key))
		return;
	MenuManager* pMenus = GameplayState::GetInstance()->getMenus();
	pMenus->setActiveMenu(TempleMenu::GetInstance());
	pMenus->setVisible(!pMenus->getVisible());
}

void show_inventory(void** params)
{
	if (Input::GetInstance()->wasKeyDown(Controls::GetInstance().open_inventory.key))
		return;
	MenuManager* pMenus = GameplayState::GetInstance()->getMenus();
	pMenus->setActiveMenu(InventoryMenu::GetInstance());
	pMenus->setVisible(!pMenus->getVisible());
}

/*virtual*/ GameplayState::~GameplayState(void)
{
	ChunkManager::Shutdown();
	Camera::ShutDown();	
	delete menus;
}

/*virtual*/ void GameplayState::Update(float dt) /*override*/
{
	// update the chunk manager
	ChunkManager::GetInstance()->Update(dt);

	// update camera
	Camera::GetInstance()->Update(dt);
	if (!menus->getVisible() && !Console::GetInstance()->isVisible() && Game::GetInstance()->getWindowFocoused())
	{
		// keep the mouse cursor in the center of the screen
		Input::GetInstance()->setIgnoreMouseMove(true);
		int scr_width = SettingsManager::GetInstance()->getSetting("ScreenWidth")._int;
		int scr_height = SettingsManager::GetInstance()->getSetting("ScreenHeight")._int;
		Input::GetInstance()->setMousePosition(scr_width / 2, scr_height / 2);
		Input::GetInstance()->setIgnoreMouseMove(false);
	}
	// update menus
	menus->Update(dt);
}

/*virtual*/ void GameplayState::Draw(void) /*override*/
{
	BlendStateManager* pBSM = BlendStateManager::GetInstance();
	pBSM->SetBlendState(pBSM->blendStates.opaque);

	GraphicsManager::GetInstance()->ClearBackBuffer(vector_4::red);

	// draw the current level
	ChunkManager::GetInstance()->Draw();

	pBSM->SetBlendState(pBSM->blendStates.alphaBlend);
	// draw any active menus
	menus->Draw();
}

/*static*/ GameplayState* GameplayState::GetInstance(void)
{
	if (!instance)
		instance = new GameplayState;
	

	return instance;
}